var marker;
var map;
var suppliers;
var selectCity;
var isLoadArea = false;

jQuery(function($){
	$("#areaSetting").click(function(){
		getParentAreas();
	});
	selectCity = $("#city option:selected").get(0).getAttribute("cityID");
});

function refresh_img_list_box()
{
	var curr_id = $("#gallery_id").val();
	var thumb_list = document.getElementById("gallery_list").getElementsByTagName("IMG");
	var input_list = document.getElementById("gallery_list").getElementsByTagName("INPUT");
	for(var i=0;i<input_list.length;i++)
	{
		if(input_list[i].value == curr_id)
		{
			thumb_list[i].className="selectborder";
		}
		else
		{
			thumb_list[i].className = "";
		}
	}

	if($("#gallery_list input").size() > 1)
		delgallery($("#gallery_list input").eq(0).val(),$("#gallery_list img").get(0));
}

function setgallery(id)
{
	$.ajax({
		  url: APP+"?"+VAR_MODULE+"=Goods&"+VAR_ACTION+"=setGallery&id="+id,
		  cache: false,
		  success:function(data)
		  {
			data = $.evalJSON(data);

			if(data.info)
			{
				alert(data.info);
			}
			else
			{
				$("#gallery_id").val(data.id);
				$("#goods_img_box").attr("src",ROOT_PATH+data.big_img);
				refresh_img_list_box();
			}
		  }
	});
}

function delgallery(id,obj)
{
	if(obj.id == "goodsimg")
	{
		delgoods(id,obj);
	}

	$.ajax({
		  url: APP+"?"+VAR_MODULE+"=Goods&"+VAR_ACTION+"=delGallery&id="+id,
		  cache: false,
		  success:function(data)
		  {
			if(data!='')
			{
				data = $.evalJSON(data);
				alert(data.info);
			}
			else
			{
				if($("#gallery_id").val()==id)
				{
					$("#gallery_id").val(0);
					$("#goods_img_box").attr("src",PUBLIC+"/Images/nopic.gif");
				}
				obj.parentNode.parentNode.removeChild(obj.parentNode);
			}
		  }
	});
}

function delgoods(id,obj)
{
	$.ajax({
		  url: APP+"?"+VAR_MODULE+"=Goods&"+VAR_ACTION+"=delGoods&id="+id,
		  cache: false,
		  success:function(data)
		  {
			if(data!='')
			{
				data = $.evalJSON(data);
				alert(data.info);
			}
			else
			{
				if($("#gallery_list input").size() == 0)
				{
					$("#gallery_id").val(0);
					$("#goods_img_box").attr("src",PUBLIC+"/Images/nopic.gif");
				}
				obj.parentNode.parentNode.removeChild(obj.parentNode);
			}
		  }
	});
}

function addSuppliers()
{
	$("#suppliersList").append($("#goodsSuppliersListHtml").html());
}

function deleteSuppliers(obj)
{
	$(obj).parent().parent().parent().parent().parent().remove();
}

function addAreas()
{
	$("#areaList").append($("#goodsAreaListHtml").html());
}

function getParentAreas()
{
	var city = $("#city option:selected").get(0).getAttribute("cityID");
	if(city != selectCity)
	{
		selectCity = city;
		$(".parent_area").html('');
		$(".child_area").html('');
	}
	else if(!isLoadArea)
	{
		isLoadArea = true;
	}
	else
		return false;

	$.ajax({
		url: APP+"?"+VAR_MODULE+"=BusinessCircle&"+VAR_ACTION+"=getArea&city_id="+city,
		cache: false,
		dataType:'json',
		success:function(data)
		{
			var html = '<option value="">该城市下没有设置商圈</option>';
			var i = 0;
			if(data.list)
			{
				html = '';
				var count = data.list.length;
				for(i;i < count;i++)
				{
					html += '<option value="'+ data.list[i].name +'">'+ data.list[i].name +'</option>';
				}
			}

			$(".parent_area").each(function(){
				if($('option',this).length == 0)
				{
					$(this).html(html);
					getChildAreas(this);
				}
			});
		}
	});
}

function deleteArea(obj)
{
	$(obj).parent().parent().parent().parent().parent().remove();
}

function getChildAreas(obj)
{
	var parent_name = $(obj).val();
	var parent = $(obj).parent().parent();
	$(".child_area",parent).html("");

	if(parent_name == '')
	{
		var parent_txt = $(obj).val();
		if($('option:selected',obj).text() == '重新设置')
		{
			$.ajax({
				url: APP+"?"+VAR_MODULE+"=BusinessCircle&"+VAR_ACTION+"=getArea&city_id="+selectCity,
				cache: false,
				dataType:'json',
				success:function(data)
				{
					var html = '<option value="">该城市下没有设置商圈</option>';
					var i = 0;
					if(data.list)
					{
						html = '';
						var count = data.list.length;
						for(i;i < count;i++)
						{
							html += '<option value="'+ data.list[i].name +'">'+ data.list[i].name +'</option>';
						}
					}

					$(obj).html(html);
					getChildAreas(obj);
				}
			});

			return false;
		}
		else
		{
			$(".child_area",parent).html("<option value=''>无子商圈</option>");
			return false;
		}
	}

	$.ajax({
		url: APP+"?"+VAR_MODULE+"=BusinessCircle&"+VAR_ACTION+"=getChildArea&city_id="+ selectCity +"&parent_name="+parent_name,
		cache: false,
		data:{"city_id":selectCity,"parent_name":parent_name},
		dataType:'json',
		success:function(data)
		{
			var html = '<option value="">该商圈下没有设置子商圈</option>';
			var i = 0;
			if(data.list)
			{
				html = '';
				var count = data.list.length;
				for(i;i < count;i++)
				{
					html += '<option value="'+ data.list[i].name +'">'+ data.list[i].name +'</option>';
				}
			}
			$(".child_area",parent).html(html);
		}
	});
}

function resetChildAreas(obj)
{
    if($("option",obj).eq(0).text() != '重新设置')
        return false;

	var parent = $(obj).parent().parent();
	var parent_name = $(".parent_area",parent).val();

	$.ajax({
		url: APP+"?"+VAR_MODULE+"=BusinessCircle&"+VAR_ACTION+"=getChildArea&city_id="+ selectCity +"&parent_name="+parent_name,
		cache: false,
		data:{"city_id":selectCity,"parent_name":parent_name},
		dataType:'json',
		success:function(data)
		{
			var html = '<option value="">该商圈下没有设置子商圈</option>';
			var i = 0;
			if(data.list)
			{
				html = '';
				var count = data.list.length;
				for(i;i < count;i++)
				{
					html += '<option value="'+ data.list[i].name +'">'+ data.list[i].name +'</option>';
				}
			}
			$(obj).html(html);
		}
	});
}

function getSuppliersPosition(obj)
{
	suppliers = $(obj).parent().parent();
	var address = $(".suppliers_map_address",suppliers).val();
	var longitude = $(".longitude",suppliers).val();
	var latitude = $(".latitude",suppliers).val();

	if(Math.abs(longitude) > 0 && Math.abs(latitude) > 0)
	{
		var latLng = new GLatLng(latitude,longitude);
		showMapWindow(latLng,"");
	}
	else if(address != "")
	{
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,function(latLng){
			if (latLng)
			{
				showMapWindow(latLng,address);
			}
			else
			{
				alert("查询不到【" + address + "】的定位信息!");
			}
		});
	}
	else
	{
		alert("请输入要查询的的定位地址!");
	}
}

function showMapWindow(latLng,address)
{
	var selfObj=$(".mapwindow");

	if(selfObj.length==0)
	{
		var html='<div class="mapwindow"><dl><dt><span>查询定位信息</span><a href="javascript:;" title="关闭">关闭</a></dt><dd><div id="googleMap"></div></dd></dl></div>';
		$("body").append(html);
		selfObj=$(".mapwindow");

		$(window).scroll(function(){
			if(selfObj.css("display") != "none")
			{
				$.windowCenter(selfObj);
				$(bgDiv).css({width:$(document).width(), height:$(document).height()});
			}
		});
	}

	var bgDiv=document.createElement("DIV");

	$("body").append(bgDiv);
	$(bgDiv).css({position:"absolute",width:$(document).width(), height:$(document).height(),top:"0",left:"0",opacity:0.8,background:"#fff",display:"none","z-index":100});
	$.windowCenter(selfObj);
	selfObj.show();
	$(bgDiv).show();
	$(bgDiv).bgiframe();

	map = new GMap2($("#googleMap").get(0));
	map.enableScrollWheelZoom();
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GMenuMapTypeControl());

	map.setCenter(latLng,13);
	marker = new GMarker(latLng,{title:address,draggable:true,bouncy:true,dragCrossMove:true});
	map.addOverlay(marker);

	$(bgDiv).click(function(){
		$(bgDiv).remove();
		selfObj.css({display:"none"});
		closeMapWindow();
	});

	$("dt a",selfObj).click(function(){
		$(bgDiv).remove();
		selfObj.css({display:"none"});
		closeMapWindow();
	});
}

function closeMapWindow()
{
	$(".longitude",suppliers).val(marker.getLatLng().lng());
	$(".latitude",suppliers).val(marker.getLatLng().lat());
	GUnload();
}